﻿// ------------------------------------------------------------------------------------
//      Copyright (c) uhavemyword@gmail.com. All rights reserved.
//      Created by Ben at 02/21/2013 13:13:53
// ------------------------------------------------------------------------------------

namespace CP.NLayer.Client.WpfClient.Common
{
    using System.Windows.Input;
    using CP.NLayer.Models;
    using CP.NLayer.Resources.UI;
    using CP.NLayer.Service.Contracts;
    using Microsoft.Practices.Prism.Commands;
    using Microsoft.Practices.Unity;

    public class EditViewModel<T> : ViewModelBase where T : ModelBase
    {
        private string _id;
        private T _item;
        private bool? _dialogResult;

        protected IEditModelService<T> _service
        {
            get { return DependencyInjection.Container.Resolve<IEditModelService<T>>(); }
        }

        public EditViewModel()
        {
            this.SaveCommand = new DelegateCommand(ExecuteSaveCommand, CanExecuteSaveCommand);
            this.CancelCommand = new DelegateCommand(ExecuteCancelCommand, CanExecuteCancelCommand);
        }

        public T Item
        {
            get { return _item; }
            set
            {
                _item = value;
                this.RaisePropertyChanged(() => this.Item);
            }
        }

        public string Id
        {
            get { return _id; }
            set
            {
                _id = value;
                this.Item = (string.IsNullOrEmpty(_id) ? _service.Create() : _service.GetById(_id));
                this.RaisePropertyChanged(() => this.Id);
            }
        }

        public bool? DialogResult
        {
            get { return _dialogResult; }
            set
            {
                if (_dialogResult != value)
                {
                    _dialogResult = value;
                    this.RaisePropertyChanged(() => this.DialogResult);
                }
            }
        }

        public string ViewHeader
        {
            get
            {
                string header = string.Empty;
                switch (Operation)
                {
                    case OperationEnum.Add:
                        header = UiResources.Add;
                        break;
                    case OperationEnum.Edit:
                        header = UiResources.Edit;
                        break;
                }
                return header;
            }
        }

        public OperationEnum Operation
        {
            get { return string.IsNullOrEmpty(Id) ? OperationEnum.Add : OperationEnum.Edit; }
        }

        public ICommand SaveCommand { get; set; }

        public ICommand CancelCommand { get; set; }

        protected virtual bool CanExecuteSaveCommand()
        {
            return true;
        }

        protected virtual void ExecuteSaveCommand()
        {
            if (!string.IsNullOrEmpty(this.Item.GetValidationErrors()))
            {
                return;
            }

            switch (Operation)
            {
                case OperationEnum.Add:
                    _service.Insert(Item);
                    break;
                case OperationEnum.Edit:
                    _service.Update(Item);
                    break;
            }
            this.DialogResult = true;
        }

        protected virtual bool CanExecuteCancelCommand()
        {
            return true;
        }

        protected virtual void ExecuteCancelCommand()
        {
            this.DialogResult = false;
        }
    }
}
